package com.gzhryc.jdbc.utils;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.jdbc.ConnectionFactory;
import com.gzhryc.jdbc.DbHelper;
import com.gzhryc.jdbc.JdbcHelper;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.jdbc.dialect.h2.H2DbHelper;
import com.gzhryc.jdbc.dialect.h2.H2JdbcHelper;
import com.gzhryc.jdbc.dialect.mysql.MysqlDbHelper;
import com.gzhryc.jdbc.dialect.mysql.MysqlJdbcHelper;
import com.gzhryc.jdbc.dialect.oracle.OracleDbHelper;
import com.gzhryc.jdbc.dialect.oracle.OracleJdbcHelper;
import com.gzhryc.jdbc.enums.DbType;

public class HelperUtils {

	public static JdbcHelper createJdbcHelper(String connectionKey){
		if(StringUtils.isBlank(connectionKey)) {
			connectionKey = MultiDBTools.getJdbcKey();
			if(StringUtils.isBlank(connectionKey)) {
				connectionKey = ConnectionFactory.getDefaultJdbcKey();
			}
		}
		DbType dbType = null;
		if(StringUtils.isNotBlank(connectionKey)) {
			dbType = ConnectionFactory.getDbType(connectionKey);
		}
		
		if(dbType != null) {
			if(DbType.mysql.equals(dbType)){
				return new MysqlJdbcHelper(connectionKey);
			}else if(DbType.oracle.equals(dbType)){
				return new OracleJdbcHelper(connectionKey);
			}else if(DbType.h2.equals(dbType)){
				return new H2JdbcHelper(connectionKey);
			}
		}
		return null;
	}
	
	public static DbHelper createDbHelper(String connectionKey){
		if(StringUtils.isBlank(connectionKey)) {
			connectionKey = MultiDBTools.getJdbcKey();
			if(StringUtils.isBlank(connectionKey)) {
				connectionKey = ConnectionFactory.getDefaultJdbcKey();
			}
		}
		DbType dbType = null;
		if(StringUtils.isNotBlank(connectionKey)) {
			dbType = ConnectionFactory.getDbType(connectionKey);
		}
		
		if(dbType != null) {
			if(DbType.mysql.equals(dbType)){
				return new MysqlDbHelper(connectionKey);
			}else if(DbType.oracle.equals(dbType)){
				return new OracleDbHelper(connectionKey);
			}else if(DbType.h2.equals(dbType)){
				return new H2DbHelper(connectionKey);
			}
		}
		return null;
	}
}
